<?php

// 主控制器类
class MainController extends Controller {
	public function __construct($model, $action) {
		parent::__construct($model, $action);
		$this->_set_model($model);
	}
	// 显示首页，并有分页功能
	public function index($cate_sel = NULL, $total_page=NULL, $start=NULL){
		if (is_null($cate_sel)) {
			$cate_sel = "all";
			$cate_str = "/all";
		} else {
			$cate_str = "/$cate_sel";
		}
		// 不带页面信息的访问，计算总页数
		if (is_null($total_page)) {
			$num = $this->_model->get_article_count($cate_sel);
			if ($num > PER_PAGE) {
				$total_page = ceil($num / PER_PAGE);
			} else {
				$total_page = 1;
			}
		}
		// 若不带起始条目序号
		if (is_null($start)) {
			$start = 0;
		}
		// 获取当前分类的编号及名称
		list($hci, $hcn) = $this->_model->get_categories();

		// 要么是 all , 要么是分类编号
		$link_conf = array(
			"base" => "MVC/main/index".$cate_str,
			"total" => $total_page,
			"start" => $start,
			"per_page" => PER_PAGE
		);

		$plinks = View::gen_page_link($link_conf);
		$r = $this->_model->get_blog_list($cate_sel, $start, PER_PAGE);
		$this->_view->set("h_cate_ids", $hci);
		$this->_view->set("h_cate_names", $hcn);
		$this->_view->set("page_title", SITE_NAME);
		$this->_view->set("entries", $r);
		$this->_view->set("page_link", $plinks);

		$this->_view->load('header.php');
		$this->_view->load('main/mainview.php');
		$this->_view->load('footer.php');
		$this->_view->display();
	}
	// 某个 ID 的详情页面
	public function article($id=NULL) {
		if (empty($id)) {
			throw new Exception("该页面不存在");
		}
		$e = $this->_model->get_article($id);
		if ($e === false) {
			throw new Exception("该文章不存在");
		}

		$t = $e['title'];
		$b = $e['body'];
		$d = $e['last_modified'];
		$c = $e['name'];
		$ci = $e['category_id'];
		$bid = $e['entry_id'];

		list($hci, $hcn) = $this->_model->get_categories();

		$err = array();
		$warn = "";
		$clean_comment = "";
		$clean_name = "";
		$clean_email = "";

		if (isset($_POST["submit"]) && $_POST["submit"] == "发表评论") {
			$dirty_name = $_POST['name'];
			$dirty_email = $_POST['email'];
			$dirty_comment = $_POST["comment"];
			// 净化 HTML, 防止 XSS 攻击
			$config = HTMLPurifier_Config::createDefault();
			$purifier = new HTMLPurifier($config);
			$clean_comment = $purifier->purify($dirty_comment);
			$clean_name = $purifier->purify($dirty_name);
			$clean_email = $purifier->purify($dirty_email);
			// 移除空的 <p></p> 对
			$clean_comment = preg_replace('/<p[^>]*>\s*?<\\/p[^>]*>/u', '', $clean_comment);
			if (empty($clean_name)) {
				$err[] = "名字不能为空。";
			}
			if (!filter_var($clean_email, FILTER_VALIDATE_EMAIL)) {
				$err[] = "Email 地址不正确。";
			}
			if (empty($clean_comment)) {
				$err[] = "评论内容不能为空。";
			}
			$warn = "评论失败，发生了以下错误：";
			// 发表成功后跳转到成功页面
			if (empty($err)) {
				if (!$this->_model->send_comment($bid, $clean_name, $clean_email,
					$clean_comment)) {
					$err[] = "评论数据库异常。";
				} else {
					$clean_comment = "";
					$clean_name = "";
					$clean_email = "";
				}
			}
		}

		$all_comments = $this->_model->get_article_comments($bid);
		if ($all_comments === false) {
			throw new Exception("获取文章评论失败");
		}

		$this->_view->set("allcom", $all_comments);
		$this->_view->set("n", $clean_name);
		$this->_view->set("e", $clean_email);
		$this->_view->set("c", $clean_comment);
		$this->_view->set("h_cate_ids", $hci);
		$this->_view->set("h_cate_names", $hcn);
		$this->_view->set("blog_id", $bid);
		$this->_view->set("warning", $warn);
		$this->_view->set("errors", $err);
		$this->_view->set("article_title", $t);
		$this->_view->set("article_body", $b);
		$this->_view->set("modified_date", $d);
		$this->_view->set("category", $c);
		$this->_view->set("cate_id", $ci);
		$this->_view->set("page_title", SITE_NAME);
		$this->_view->load("header.php");
		$this->_view->load("main/articleview.php");
		$this->_view->load("main/comment.php");
		$this->_view->load("footer.php");
		$this->_view->display();
	}

}